<template>
  <div>
    <div class="layout">
      <div>
        <BackNav left-text="返回" left-arrow title="个人资料"></BackNav>
      </div>
      <div class="top"></div>
      <div class="wrapper">
        <div class="main">
          <div class="list-item">
            <div class="left">头像</div>
            <div class="text">
              <img :src="userInfo.userImg" class="auto-img" alt="" />
            </div>
          </div>
          <div class="list-item">
            <div class="left">用户id</div>
            <div class="txt">{{ userInfo.userId }}</div>
          </div>
          <div class="list-item">
            <div class="left" @click="modifyNickname">昵称</div>
            <div class="txt" v-if="!show">
              <input type="text" v-model="newname" />
            </div>
            <div class="txt" v-if="show">{{ userInfo.nickName }}</div>
          </div>
          <div class="list-item">
            <div class="left" @click="modifyintrduce">简介</div>
            <div class="txt" v-if="!isshow">
              <input type="text" v-model="newdec" />
            </div>
            <div class="txt" v-if="isshow">{{ newdec }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BackNav from "../components/BackNav";
export default {
  components: {
    BackNav,
  },
  data() {
    return {
      userInfo: [],
      show: true,
      newname: "",
      newdec: "",
      isshow: true,
    };
  },
  created() {
    this.modifyintrduce1();
    this.getPerson();
    this.newdec=JSON.parse(sessionStorage.getItem('qian'));
  },
  methods: {
    //   获取个人资料
    getPerson() {
      this.axios({
        method: "get",
        url: "/findAccountInfo",
      })
        .then((res) => {
          
          if (res.data.code == "B001") {
            this.userInfo = res.data.result[0];
          }
        })
        .catch((err) => {
          
        });
    },
    // 修改昵称
    modifyNickname() {
      this.show = !this.show;
      this.axios({
        url: "/updateNickName",
        method: "post",
        data: {
          nickName: this.newname,
        },
      })
        .then((res) => {
          
          if (res.data.code == "C001") {
            this.userInfo.nickName = this.newname;
          }
        })
        .catch((err) => {
          
        });
    },
    // 修改简介
    modifyintrduce() {
      this.isshow = !this.isshow;
      if (this.isshow) {
        this.axios({
          url: "/updateDesc",
          method: "post",
          data: {
            desc: this.newdec,
          },
        })
          .then((res) => {
            
            if (res.data.code == "D001") {
              this.userInfo.dec = this.newdec;
              sessionStorage.setItem("qian",JSON.stringify(this.userInfo.dec))
            }
          })
          .catch((err) => {
            
          });
      }
    },
    modifyintrduce1() {
      this.axios({
        url: "/updateDesc",
        method: "post",
        data: {
          desc: this.newdec,
        },
      })
        .then((res) => {
          
          if (res.data.code == "D001") {
            this.userInfo.dec = this.newdec;
           
          }
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>
<style lang="less" scoped>
.layout {
  background: #f0f0f0;
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  .top {
    height: 100px;
    background: #0c43ba;
  }
  .wrapper {
    width: 90%;
    background: #fff;
    position: relative;
    top: -20px;
    border-radius: 10px;
    margin: 0 auto;
    .main {
      padding: 10px;
    }
    .list-item {
      border-bottom: 1px dashed #f0f0f0;
      padding: 10px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-wrap: wrap;
      &:last-child {
        padding: 10px 0 30px 0;
        border-bottom: 0;
      }
      .left {
        font-size: 14px;
      }
      .text {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        overflow: hidden;
      }
      .txt {
        overflow: hidden;
        margin-left: auto;
        color: #969799;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 14px;
      }
    }
  }
}
</style>